﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Stima
{
    public class Car : DrawableGameComponent
    {

        public Vector2 position;
        public int type;
        public int direction;
        public Texture2D texture;
        public static MouseState mouseState;
        Vector2 move_to;

        public Car(Game1 game,Vector2 pos,int typ, int dir, Texture2D tex) : base(game) 
        {
            position = pos;
            move_to = pos;
            type = typ;
            direction = dir;
            texture = tex;
        }

        public int getType() 
        {
            return type;
        }

        public int getDir()
        {
            return direction;
        }

        public float getPosX()
        {
            return position.X; 
        }
        public float getPosY()
        {
            return position.Y;
        }

        public override void Update(GameTime gameTime)
        {
            if (direction == 1)
            {
                if (this.type == 2)
                {
                    if (this.position.Y != this.move_to.Y && this.move_to.Y >= 0 && this.move_to.Y <= 400)
                        this.position.Y = this.move_to.Y;
                }
                else
                {
                    if (this.position.Y != this.move_to.Y && this.move_to.Y >= 0 && this.move_to.Y <= 300)
                        this.position.Y = this.move_to.Y;
                }
            }
            else
            {
                if (this.type == 2)
                {
                    if (this.position.X != this.move_to.X && this.move_to.X >= 0 && this.move_to.X <= 400)
                        this.position.X = this.move_to.X;
                }
                else
                {
                    if (this.position.X != this.move_to.X && this.move_to.X >= 0 && this.move_to.X <= 300)
                        this.position.X = this.move_to.X;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            ((Game1)Game).SpriteBatch.Draw(texture, position, Color.White);

            base.Draw(gameTime);
        }

        public bool isClick(int x, int y)
        {
            if (type == 3)
            {
                if (direction == 1)
                {
                    if (x >= (position.X) && x < (position.X + 100) && y >= (position.Y) && y < (position.Y + 300))
                        return true;
                    else return false;
                }
                else
                {
                    if (x >= (position.X * 100) && x < (position.X + 300) && y >= (position.Y) && y < (position.Y + 100))
                        return true;
                    else return false;
                }
            }
            else 
            {
                if (direction == 1)
                {
                    if (x >= (position.X) && x < (position.X + 100) && y >= (position.Y) && y < (position.Y + 200))
                        return true;
                    else return false;
                }
                else
                {
                    if (x >= (position.X) && x < (position.X + 200) && y >= (position.Y) && y < (position.Y + 100))
                        return true;
                    else return false;
                }
            }
 
        }

        public void moveTo(float x, float y) 
        {
            this.move_to = new Vector2(x - (texture.Width/2), y - (texture.Height/2));
        }

        public bool IsCollider(Vector2 pos, float radius)
        {
            if (Vector2.DistanceSquared(this.position, pos) <= radius * radius)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }

        private void CheckCollision()
        {
            for (int i = 0; i < ((Game1)Game).car.Length; i++)
            {
                if (((Game1)Game).car[i] != this)
                {
                    if (((Game1)Game).car[i].IsCollider(position, 10))
                    { 
                    }
                }
            }
        }
    }
}
